package http.util;

import main.robClass;
import okhttp3.Cookie;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ParseUtil {
    //注意只能解析val
    public static String extractCharacters(Elements elements) {

        if (elements == null || elements.size() == 0) {
            robClass.logger.info("解析失败: 数据可能不存在 | 页面重定向");
            return null;
        }
        robClass.logger.info(elements);
        for (Iterator<Element> it = elements.iterator(); it.hasNext(); ) {
            Element resMessage = it.next();
            return extractCharacters(resMessage.val());
        }
        return null;
    }

    public static String extractCharacters(String string) {

        if (string == null) {
            robClass.logger.info("解析失败: 数据可能不存在 | 页面重定向");
            return null;
        }

        Pattern pattern = Pattern.compile("[\u4e00-\u9fa5]");
        StringBuilder messageStr = new StringBuilder();
        Matcher matcher = pattern.matcher(string);
        while (matcher.find()) {
            messageStr.append(matcher.group());
        }
//        messageStr.append("\r\n");

        if (messageStr.length() > 0) {
            //robClass.logger.info("解析成功: " + messageStr);
            return messageStr.toString();
        } else {
            // robClass.logger.info("解析失败: " + "匹配不到数据");
            return null;
        }
    }

    public static String parsePERobScript(String responseHtml) {
        if (responseHtml == null) {
            return null;
        }
        Document doc = Jsoup.parse(responseHtml);
        if (doc.select("script[language]").size() == 0) {
            return null;
        }
        Element element = doc.select("script[language]").get(0);
        return extractCharacters(element.data());
    }

    public static String parseCommonRobScript(String responseHtml) {
        if (responseHtml == null) {
            return null;
        }
        Document doc = Jsoup.parse(responseHtml);
        Elements alert = doc.select("script");
        if (alert != null) {
            for (int ii = 0; ii < 2; ii++) {
                if (alert.size() > 0 && alert.get(ii).html() != null) {
                    if (alert.get(ii).html().split("alert\\('").length > 1) {
                        return alert.get(ii).html().split("alert\\('")[1].split("'")[0];
                    }

                }
            }
        }
        return "解析原因失败!";
    }


    public static String parseStudentNameSpan(String responseHtml) {
        if (responseHtml == null) {
            return null;
        }
        Document doc = Jsoup.parse(responseHtml);
        Element element = doc.getElementById("xhxm");
        return element.text().split("同学")[0];

    }

    public static String parseScript(String responseHtml) {
        if (responseHtml == null) {
            return null;
        }
        Document doc = Jsoup.parse(responseHtml);
        if (doc.select("script[language]").size() == 0) {
            return null;
        }
        Element element = doc.select("script[language]").last();
        return extractCharacters(element.data());
    }

    public static String parseElement(String responseHtml, String element) {
        Document doc = Jsoup.parse(responseHtml);
        Elements elements = doc.select(element);
        return extractCharacters(elements);
    }

    public static boolean parseObjectMoved(String responseHtml) {
        Document doc = Jsoup.parse(responseHtml);
        Elements linkElements = doc.select(" head > title");
        Element e = linkElements.first();
        if (e == null) {
            return false;
        }
        return e.text().equalsIgnoreCase("Object moved");
    }
}
